package com.hdoit.ares.logical.insu.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.insu.dao.IFanhuaCarDao;
import com.hdoit.ares.logical.insu.mapper.FanhuaCarMapper;
import com.hdoit.ares.logical.insu.model.FanhuaCar;
import com.hdoit.ares.logical.viewmodel.insu.FanhuaCarSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年12月20日 17:27:00
 * @comment
 */
@Component
public class FanhuaCarDaoImpl extends SuperDao<FanhuaCar> implements IFanhuaCarDao{

	private FanhuaCarMapper fahuaCarMapper;
	public FanhuaCarMapper getFahuaCarMapper() {
		return fahuaCarMapper;
	}
	@Resource
	public void setFahuaCarMapper(FanhuaCarMapper fahuaCarMapper) {
		this.fahuaCarMapper = fahuaCarMapper;
	}
	
	@Override
	public List<FanhuaCar> queryAllCarInsurance(String customerId) {
		
	    return fahuaCarMapper.queryAllCarInsurance(customerId);
	}
	@Override
	public FanhuaCar queryCarInsuDetail(String orderId) {
		
		return fahuaCarMapper.queryCarInsuDetail(orderId);
	}
	@Override
	public FanhuaCar queryCarInsuDetailBytaskId(String taskId,String taskState) {

		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(FanhuaCar.class)
						.andEq("taskId",taskId)
						.andEq("taskState",taskState)
						.addOrder("createDate", OrderEnum.desc);
	    return queryModel(finderExpression);
	}
	
	@Override
	public List<FanhuaCar> queryAllCarInsu(String customerId) {	    
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(FanhuaCar.class,"a")
						.andEq("a.customerId",customerId)
						.andWhere("a.createDate in ("+selectfinderExpression().toSql()+")")
						.addOrder("createDate", OrderEnum.desc);
	    return queryList(finderExpression);
	}

	@Override
	public List<FanhuaCar> queryAreadyCarInsu(String customerId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(FanhuaCar.class,"a")
						.andEq("customerId",customerId)
						.andWhere("TIMESTAMPDIFF(SECOND,a.efcInsureInfoStartDate,NOW())<=0")
						.andWhere("a.createDate in ("+selectfinderExpression().toSql()+")")
						.andEq("taskState", "33")
						.addOrder("createDate", OrderEnum.desc);
	    return queryList(finderExpression);
	}
	
	@Override
	public List<FanhuaCar> queryYesCarInsu(String customerId) {

		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(FanhuaCar.class,"a")
						.andEq("customerId",customerId)
						.andWhere("NOW() BETWEEN a.efcInsureInfoStartDate and a.efcInsureInfoEndDate")
						.andWhere("a.createDate in ("+selectfinderExpression().toSql()+")")
						.andEq("taskState", "33")
						.addOrder("createDate", OrderEnum.desc);
	    return queryList(finderExpression);
	}
	
	@Override
	public List<FanhuaCar> queryNoCarInsu(String customerId) {		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(FanhuaCar.class,"a")
						.andEq("customerId",customerId)
						.andWhere("TIMESTAMPDIFF(SECOND,a.efcInsureInfoEndDate,NOW())>=0")
						.andWhere("a.createDate in ("+selectfinderExpression().toSql()+")")
						.andEq("taskState", "33")
						.addOrder("createDate", OrderEnum.desc);
	    return queryList(finderExpression);
	}
	@Override
	public List<FanhuaCar> queryList(FanhuaCarSearch fanhuaCarSearch, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(FanhuaCar.class)
						.addOrder("createDate", OrderEnum.desc);
		if(pagerModel == null){
			return queryList(finderExpression, fanhuaCarSearch);
		}		
		return queryList(finderExpression, fanhuaCarSearch,pagerModel);
	}
	
	public IFinderExpression selectfinderExpression(){
		IFinderExpression selectfinderExpression=FinderExpressionFactory.createExpression();
		selectfinderExpression.select("max(createDate)")
						.from(FanhuaCar.class,"b")
						.andWhere("b.taskState='19'")
						.orWhere("b.taskState='20'")
						.orWhere("b.taskState='23'")
						.orWhere("b.taskState='25'")
						.orWhere("b.taskState='33'")
						.addGroup("b.taskId");
		return selectfinderExpression;
	}
	
	@Override
	public FanhuaCar queryFanhuaCar(String oid) {
		return fahuaCarMapper.queryFanhuaCar(oid);
	}
	
}